//
//  myMusicFMView.m
//  myMusicApp
//
//  Created by 11_wyq on 15/6/16.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "myMusicFMView.h"

@interface myMusicFMView ()

@property (retain ,nonatomic) MBProgressHUD *MBP;

@end


@implementation myMusicFMView



-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubviews];
    }
    return self;
}

-(void)createSubviews
{

 
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    
    //设置item大小
    flowLayout.itemSize = CGSizeMake(110 * WIDTH, 110 * HEIGHT);
    //section距离边界的间距
    flowLayout.sectionInset = UIEdgeInsetsMake(20 * WIDTH, 10 * HEIGHT , 20 * WIDTH, 10 *HEIGHT);
    //头部视图区域大小
    flowLayout.headerReferenceSize = CGSizeMake(self.frame.size.width, 170 * HEIGHT);
    //底部视图区域大小
    flowLayout.footerReferenceSize = CGSizeMake(self.frame.size.width, 130 );
    
    
    
    //集合视图
    self.CollectionView = [[UICollectionView alloc]initWithFrame:[[UIScreen mainScreen]bounds] collectionViewLayout:flowLayout];
    self.CollectionView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.CollectionView];
    self.CollectionView.delegate = self;
    self.CollectionView.dataSource = self;
    self.CollectionView.backgroundColor = [UIColor blackColor];
    //注册item
    [self.CollectionView registerClass:[MusicCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
    //注册头部
    [self.CollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerReuse"];
    //注册尾部
    [self.CollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footReuse"];
    
    
    
    //垂直:各小方格之间的列间距
    flowLayout.minimumInteritemSpacing = 5 * WIDTH;
    
    //行间距
    flowLayout.minimumLineSpacing = 50 * HEIGHT;
    
    

    self.ScrollViewArray = [NSMutableArray array];
    self.array = [NSMutableArray array];
    
//    //菊花
//    self.MBP = [MBProgressHUD showHUDAddedTo:self animated:YES];
//    self.MBP.labelText = @"拼命加载中...";
//    self.MBP.xOffset = 0.0;
//    self.MBP.yOffset = -50.0;//改变菊花Y轴偏移量
//    [self.MBP show:YES];

    [GiFHUD setGifWithImageName:@"xiaohuangren.gif"];
    [GiFHUD show];
    
    
    [self addHeader];//下拉刷新
    
    
    
    [self getMassage];
 
    [self getScrollViewMassage];


}

-(void)addHeader
{
    __block myMusicFMView *FMList=self;
    
    
    self.CollectionView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self getScrollViewMassage];
        [self getMassage];
        
        
        
    }];
    
    
}

//设置头部和底部区域内容
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
  
    
    
    //kind是显示区域类型
    if (kind == UICollectionElementKindSectionHeader) {
        
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerReuse" forIndexPath:indexPath];
        headerView.backgroundColor = [UIColor blackColor];

        headerView.userInteractionEnabled = YES;
        //头视图上添加定时滚动条
        [headerView addSubview:self.MusicFMScrollV];
     return  headerView;
        
        
    }else{
        
        UICollectionReusableView *footView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footReuse" forIndexPath:indexPath];
        footView.backgroundColor = [UIColor blackColor];
        return footView;
    }
    
}





//点击某个item
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if ([DataHandle shareHandleController].monitorNetwork == NO) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您的网络不给力,请检查网络设置" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }

    
    
    MusicFMModel *music = [self.array objectAtIndex:indexPath.row];
    
    
    
    [self.myDelegate pushView:music.tname];
    
    
    
    
    
    
}

//设置分区个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}


//设置item的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.array.count;
    
}

//布局cell
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //直接从系统的重用池里取cell
    MusicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];

  
    cell.FMModel = [self.array objectAtIndex:indexPath.row];

    //渐变效果
    cell.layer.shadowColor = [[UIColor blackColor]CGColor];
    cell.alpha = 0.1;
    [UIView beginAnimations:@"rotation" context:NULL];
    [UIView setAnimationDuration:1.5];//时间
    cell.alpha = 1;
    [UIView commitAnimations];
    return cell;
    
}




//获取瀑布流类型及图片
-(void)getMassage
{
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    
    [[DataHandle shareHandleController]openDB];
    [[DataHandle shareHandleController]createMusicFMTable];
    
    
    NSString *string =[NSString stringWithFormat:@"http://mobile.ximalaya.com/m/category_tag_list?category=music&device=iPhone&type=album"];
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        //  NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *list = [dic objectForKey:@"list"];
        
        [[DataHandle shareHandleController]dropMusicFMTable];
        [[DataHandle shareHandleController]createMusicFMTable];
        self.array = [NSMutableArray array];
        for (NSMutableDictionary *smallDic in list) {
           
            MusicFMModel *FMModel = [[MusicFMModel alloc]init];
            FMModel.tname = [smallDic objectForKey:@"tname"];
            FMModel.cover_path = [smallDic objectForKey:@"cover_path"];
          
            [self.array addObject:FMModel];
            [[DataHandle shareHandleController]insertMusicFM:FMModel];
            
        }
        
        [self.CollectionView reloadData];
      //   [GiFHUD dismiss];//动态图隐藏
      [self.CollectionView.header endRefreshing];//结束下拉
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您的网络不给力,请检查网络设置" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
        
        self.array = [[DataHandle shareHandleController]selectMusicFM];
        [self.CollectionView reloadData];
        // [self.MBP hide:YES];//菊花隐藏
         [GiFHUD dismiss];
        NSLog(@"失败==== %@",error);
        
    }];
    
    
    
}



//获取滑动视图图片
-(void)getScrollViewMassage
{
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    [[DataHandle shareHandleController]openDB];
    [[DataHandle shareHandleController]createMusicFMPicTable];
    
    NSString *string =[NSString stringWithFormat:@"http://mobile.ximalaya.com/m/category_focus_image?categoryId=2&device=iPhone&version=3.1.43"];
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        //  NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *list = [dic objectForKey:@"list"];
    
        [[DataHandle shareHandleController]dropMusicFMPicTable];
        [[DataHandle shareHandleController]createMusicFMPicTable];
        
        
        for (NSMutableDictionary *smallDic in list) {
            MusicFMModel *myFMModel = [[MusicFMModel alloc]init];
            myFMModel.myId = [smallDic objectForKey:@"id"];
            myFMModel.pic = [smallDic objectForKey:@"pic"];
            myFMModel.trackId = [smallDic objectForKey:@"trackId"];
            
            [self.ScrollViewArray addObject:myFMModel];
            [[DataHandle shareHandleController]insertMusicFMPic:myFMModel];
            [myFMModel release];
            
            
        }
      
        
        self.MusicFMScrollV = [[MusicFMScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 4.45) array:self.ScrollViewArray];
        self.MusicFMScrollV.array = [NSMutableArray arrayWithArray:self.ScrollViewArray];
        
        
        
        //刷新
        [self.CollectionView reloadData];
       // [self.MBP hide:YES];//菊花隐藏
         [GiFHUD dismiss];
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        self.ScrollViewArray = [[DataHandle shareHandleController]selectMusicFMPic];
        
        self.MusicFMScrollV = [[MusicFMScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 4.45) array:self.ScrollViewArray];
        self.MusicFMScrollV.array = [NSMutableArray arrayWithArray:self.ScrollViewArray];
       
        
          [self.CollectionView reloadData];
        // [self.MBP hide:YES];//菊花隐藏
         [GiFHUD dismiss];
        
        NSLog(@"失败==== %@",error);
        
    }];
}






@end
